package com.minimalist.lindi.controller;

import com.minimalist.common.config.mybatis.bo.PageResp;
import com.minimalist.common.entity.lindi.vo.tradeapplicationconfirmletter.LindiTradeApplicationConfirmLetterQueryVO;
import com.minimalist.common.entity.lindi.vo.tradeapplicationconfirmletter.LindiTradeApplicationConfirmLetterVO;
import com.minimalist.common.response.R;
import com.minimalist.common.utils.Add;
import com.minimalist.common.utils.Update;
import com.minimalist.lindi.service.LindiTradeApplicationConfirmLetterService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 林地指标交易平台_交易申请确认函 前端控制器。
 *
 * @author 11987
 * @since 2025-06-06
 */
@Validated
@RestController
@Tag(name = "林地指标交易平台_交易申请确认函管理")
@RequestMapping("/lindi/tradeApplicationConfirmLetter")
public class LindiTradeApplicationConfirmLetterController{

    @Autowired
    private LindiTradeApplicationConfirmLetterService tradeApplicationConfirmLetterService;

    @PostMapping("/addTradeApplicationConfirmLetter")
    @Operation(summary = "添加交易申请确认函")
    public R<?> addTradeApplicationConfirmLetter(
            @RequestBody @Validated(Add.class)
            LindiTradeApplicationConfirmLetterVO tradeApplicationConfirmLetterVO){
        tradeApplicationConfirmLetterService.addTradeApplicationConfirmLetter(tradeApplicationConfirmLetterVO);
        return R.ok();
    }

    @DeleteMapping("/deleteTradeApplicationConfirmLetterByConfirmLetterId")
    @Operation(summary = "删除交易申请确认函 -> 根据确认函ID删除")
    public R<?> deleteTradeApplicationConfirmLetterByConfirmLetterId(
            @RequestParam("confirmLetterId")
            @NotNull(message = "确认函ID不能为空")
            @Parameter(name = "confirmLetterId", required = true, description = "确认函ID")
            Long confirmLetterId){
        tradeApplicationConfirmLetterService.deleteTradeApplicationConfirmLetterByConfirmLetterId(confirmLetterId);
        return R.ok();
    }

    @PutMapping("/updateTradeApplicationConfirmLetterByConfirmLetterId")
    @Operation(summary = "修改交易申请确认函")
    public R<?> updateTradeApplicationConfirmLetterByConfirmLetterId(
            @RequestBody @Validated(Update.class)
            LindiTradeApplicationConfirmLetterVO tradeApplicationConfirmLetterVO){
        tradeApplicationConfirmLetterService.updateTradeApplicationConfirmLetterByConfirmLetterId(tradeApplicationConfirmLetterVO);
        return R.ok();
    }

    @GetMapping("/getPageTradeApplicationConfirmLetterList")
    @Operation(summary = "查询交易申请确认函列表(分页)")
    public R<PageResp<LindiTradeApplicationConfirmLetterVO>> getPageTradeApplicationConfirmLetterList(
            LindiTradeApplicationConfirmLetterQueryVO queryVO){
        PageResp<LindiTradeApplicationConfirmLetterVO> pageResp = tradeApplicationConfirmLetterService.getPageTradeApplicationConfirmLetterList(queryVO);
        return R.ok(pageResp);
    }

    @GetMapping("/getTradeApplicationConfirmLetterByConfirmLetterId/{confirmLetterId}")
    @Operation(summary = "根据确认函ID查询交易申请确认函")
    public R<LindiTradeApplicationConfirmLetterVO> getTradeApplicationConfirmLetterByConfirmLetterId(
            @PathVariable(value = "confirmLetterId")
            @NotNull(message = "确认函ID不能为空")
            @Parameter(name = "confirmLetterId", description = "确认函ID", required = true)
            Long confirmLetterId){
        LindiTradeApplicationConfirmLetterVO vo = tradeApplicationConfirmLetterService
                .getTradeApplicationConfirmLetterByConfirmLetterId(confirmLetterId);
        return R.ok(vo);
    }

    @GetMapping("/getTradeApplicationConfirmLetterByTradeApplicationId/{tradeApplicationId}")
    @Operation(summary = "根据交易申请ID查询当前确认函")
    public R<LindiTradeApplicationConfirmLetterVO>
    getTradeApplicationConfirmLetterByTradeApplicationId(
            @PathVariable(value = "tradeApplicationId")
            @NotNull(message = "交易申请ID不能为空")
            @Parameter(name = "tradeApplicationId", description = "交易申请ID", required = true)
            Long tradeApplicationId){
        LindiTradeApplicationConfirmLetterVO vo = tradeApplicationConfirmLetterService
                .getTradeApplicationConfirmLetterByTradeApplicationId(tradeApplicationId);
        return R.ok(vo);
    }

    @GetMapping("/getTradeApplicationConfirmLetterListByTradeApplicationId/{tradeApplicationId}")
    @Operation(summary = "根据交易申请ID查询交易申请确认函列表")
    public R<List<LindiTradeApplicationConfirmLetterVO>>
    getTradeApplicationConfirmLetterListByTradeApplicationId(
            @PathVariable(value = "tradeApplicationId")
            @NotNull(message = "交易申请ID不能为空")
            @Parameter(name = "tradeApplicationId", description = "交易申请ID", required = true)
            Long tradeApplicationId){
        List<LindiTradeApplicationConfirmLetterVO> voList =
                tradeApplicationConfirmLetterService
                        .getTradeApplicationConfirmLetterListByTradeApplicationId(
                                tradeApplicationId);
        return R.ok(voList);
    }

    @PostMapping("/generateConfirmLetter")
    @Operation(summary = "生成确认函")
    public R<LindiTradeApplicationConfirmLetterVO> generateConfirmLetter(
            @RequestParam("tradeApplicationId")
            @NotNull(message = "交易申请ID不能为空")
            @Parameter(name = "tradeApplicationId", required = true, description = "交易申请ID")
            Long tradeApplicationId,
            @RequestParam("confirmLetterType")
            @NotNull(message = "确认函类型不能为空")
            @Parameter(name = "confirmLetterType", required = true, description = "确认函类型")
            String confirmLetterType,
            @RequestParam("createId") Long createId,
            @RequestParam("tenantId") Long tenantId){
        LindiTradeApplicationConfirmLetterVO vo =
                tradeApplicationConfirmLetterService.generateConfirmLetter(
                        tradeApplicationId, confirmLetterType, createId, tenantId);
        return R.ok(vo);
    }

    @PostMapping("/deleteTradeApplicationConfirmLetterFileByFileId")
    @Operation(summary = "删除交易申请确认函文件 -> 根据文件ID删除")
    public R<?> deleteTradeApplicationConfirmLetterFileByFileId(
            @RequestParam("fileId")
            @NotNull(message = "文件ID不能为空")
            @Parameter(name = "fileId", description = "文件ID", required = true)
            Long fileId){
        tradeApplicationConfirmLetterService.deleteTradeApplicationConfirmLetterFileByFileId(fileId);
        return R.ok();
    }
}